// pages/test1/test1_button.js
var api = require("../../config/api.js")
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryList: [],
    active_UserList: [],
    hot_articleList: [],
    newList: [],
    themeList: [],
    essenceList: [],
    select: [],
    link: '欢迎大家使用<游戏尘缘>社区微信小程序, 现在小程序还处于测试开发阶段,我们相信能做出最好的微信小程序社区。',
    value: "",
    active: 0,
    activeName: "",
  },
  // 查看文章详情
  ViewDetails:function(e){
    var articleId = e.target.dataset.id
    console.log(e.target.dataset.id)
    wx.navigateTo({
      url: "../../pages/articledetail/articledetail?"+'articleId='+articleId,
    })
  },


  // 热门文章查询
  HotArticleQuery: function () {
    console.log('热门文章查询', api.HomeAPI + 'hotArticle/')
    wx.request({
      url: api.HomeAPI + 'hotArticle/',
      data: {
        query_type: '-hot_count',
        token: app.globalData.token
      },
      dataType: 'json',
      method: 'get',
      responseType: 'text',
      success: (result) => {
        console.log(result.data)
        this.setData({ //JSON.stringify将数据转为json
          hot_articleList: result.data
          // hot_articleList:JSON.stringify(result.data)
          // hot_articleList:JSON.stringify(result.data)
        })
        // console.log(['aaa', 'abc'])
        // console.log('typeof', typeof [' ', ' '])
        // console.log('typeof', typeof result)
        console.log('data.hot_articleList[0]', this.data.hot_articleList[0])
        // console.log(typeof(result.data))
        // console.log(this.data)
      },
      fail: (res) => { },
      complete: (res) => { },
    })
  },
  onChange_bottom(event) {
    // event.detail 的值为当前选中项的索引
    this.setData({ active: event.detail });
  },
  onChange(e) {
    this.setData({
      value: e.detail,
    });
  },
  onChange2(event) {
    this.setData({
      activeName: event.detail,
    });
  },
  onChangeSecondLayerLabel(event) {
    this.setData({
      activeName: event.detail,

    });

    console.log('event.detail;', event.detail)
  },
  onSearch() {
    // var value = e.target.dataset.id
    console.log(this.data.value)
    app.globalData.value=this.data.value,
    wx.switchTab({
      url: "../dynamic/dynamic?",
      // url: "../../pages/dynamic/dynamic.wxml?"+'value='+this.data.value,
    })
  },
  onClick(event) {
    var query_type = `${event.detail.name}`;
    console.log('文章查询', api.HomeAPI + 'NewestArticle/',query_type)
    wx.request({
      url: api.HomeAPI + 'hotArticle/',
      data: {
        query_type: query_type,
        token: app.globalData.token
      },
      dataType: 'json',
      method: 'get',
      responseType: 'text',
      success: (result) => {
        console.log(result.data)
        this.setData({ //JSON.stringify将数据转为json字符串
          hot_articleList: result.data
        })
        // console.log('文章查询data.hot_articleList[0]', title, this.data.hot_articleList[0])
      },
      fail: (res) => { },
      complete: (res) => { },
    })
  },
  onClickHot() {
    this.HotArticleQuery()
  },
  onClickNewest() {
    console.log('最新文章查询', api.HomeAPI + 'NewestArticle/')
    wx.request({
      url: api.HomeAPI + 'hotArticle/',
      data: {
        type: 'create_time',
        token: app.globalData.token
      },
      dataType: 'json',
      method: 'get',
      responseType: 'text',
      success: (result) => {
        console.log(result.data)
        this.setData({ //JSON.stringify将数据转为json字符串
          hot_articleList: result.data
        })
        console.log('最新文章查询data.hot_articleList[0]', this.data.hot_articleList[0])
      },
      fail: (res) => { },
      complete: (res) => { },
    })
  },
  onClickAppreciate() {
    console.log('onClickAppreciate')
  },
  onClickEssence() {
    console.log('onClickAppreciate')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  ClickActiveUser: function () {
    console.log('活跃用户查询', api.HomeAPI + 'active_user/')
    wx.request({
      url: api.HomeAPI + 'active_user/',
      data: {
        token: app.globalData.token
      },
      dataType: 'json',
      method: 'get',
      responseType: 'text',
      success: (result) => {
        console.log(result.data)
        this.setData({
          active_UserList: result.data

        })
        console.log('active_UserList', this.data.active_UserList[0].hot_count)
      },
      fail: (res) => { },
      complete: (res) => { },
    })

  },
  // 分类查询
  CategoryView: function () {

    console.log('分类查询', api.HomeAPI + 'plate/')
    wx.request({
      url: api.HomeAPI + 'plate/',
      data: {
        token: app.globalData.token
      },
      dataType: 'json',
      method: 'get',
      responseType: 'text',
      success: (result) => {
        console.log(result.data)
        this.setData({
          categoryList: result.data

        })
        console.log('categoryList', this.data.categoryList[0].hot_count)
        console.log(typeof (result.data))
      },
      fail: (res) => { },
      complete: (res) => { },
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('生命周期函数--监听页面显示')
    this.CategoryView()
    this.HotArticleQuery()
    this.ClickActiveUser()
    console.log('this.data', this.data)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },
  // onOpen(event) {
  //   Toast(`展开: ${event.detail}`);
  // },
  // onClose(event) {
  //   Toast(`关闭: ${event.detail}`);
  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})